
<script>
import { onMounted, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { getsearchdetails } from "../api/search.js";
import { Toast } from 'vant';
export default {
  emits: ["getkeyword"],
  setup(props, { emit }) {
    const list = reactive({
      hotlist: "", //猜你喜欢数组
      extendlist: "", //热搜榜
      collectionData: null,
    });
    let Retreat = () => {};
    const router = useRouter();
    let jupm = () => {
      router.push({
        name: "searchlist",
      });
    };
    //删除本地存储
    let del = () => {
      list.collectionData = [];
      let collection1 = [];
      collection1 = JSON.stringify(collection1);
      window.localStorage.setItem("search", collection1);
      Toast("删除成功")
    };
    //本地历史搜索
    let getkeyword = (text) => {
      emit("getkeyword", text);
      router.push({
        name: "resultView",
      });
    };

    onMounted(() => {
      //本地存储历史搜索
      if (window.localStorage.getItem("search") == null) {
        window.localStorage.setItem("search", "[]");
      }
      //获取本地存储数组
      let collection = window.localStorage.getItem("search");
      collection = JSON.parse(collection);
      list.collectionData = collection;
      
      //请求
      getsearchdetails().then((data) => {
        console.log(data.data.lists);
        list.hotlist = data.data.lists.interest;
        list.hotlist.lists = list.hotlist.lists.slice(0, 9);
        list.extendlist = data.data.lists.hot;
        console.log(list.extendlist);
      });
    });
    return {
      Retreat,
      jupm,
      del,
      getkeyword,
      ...toRefs(list),
    };
  },
};
</script>
<template>
  <!-- 搜索 -->
  <div class="searchView">
    <!-- 搜索框 -->
    <div class="search">
      <div class="left" @click="$router.go(-1)">
        <i class="iconfont icon-zuojiantou-copy"></i>
      </div>
      <div class="center" @click="jupm">
        <input type="text" placeholder="" />
        <i class="iconfont icon-sousuo"></i>
      </div>
      <!-- resultView -->
      <router-link custom to="/searchlist" v-slot="{ href, navigate }">
        <div :href="href" @click="navigate" class="right">搜索</div>
      </router-link>
    </div>

    <!-- 历史数据 -->
    <div class="history">
      <h4>
        <span>搜索历史</span><i class="iconfont icon-del" @click="del"> </i>
      </h4>
      <ul>
        <li
          v-for="item in collectionData"
          :key="item"
          @click="getkeyword(item)"
        >
          {{ item }}
        </li>
      </ul>
    </div>
    <!-- 推荐搜索 -->
    <div class="history history1">
      <h4>{{ hotlist.name }}</h4>
      <ul>
        <li
          :class="[{ con: index == 0 }]"
          v-for="(item, index) in hotlist.lists"
          :key="item.boolId"
        >
          <router-link
            custom
            :to="'/details?id=' + item.bookId"
            v-slot="{ href, navigate }"
          >
            <i class="iconfont icon-ziyuan" v-show="index == 0"></i
            ><p :href="href" @click="navigate">{{ item.bookName }}</p>
          </router-link>
        </li>
      </ul>
    </div>
    <!-- 热搜榜 -->
    <div class="Hotsearch">
      <div class="center">
        <h3>
          <span>热搜榜</span
          >
          <router-link
            custom
            to="leaderboard"
            v-slot="{ href, navigate }"
          >
          <a :href="href" @click="navigate">更多榜单 <i class="iconfont icon-youjiantou-copy"></i></a>
          </router-link>
        </h3>
        <ul>
          <li v-for="(item, index) in extendlist.lists" :key="item.bookId">
            <div class="logo" v-if="index == 0">
              <span>{{ index + 1 }}</span
              ><img src="../assets/img/rank_book_top_1.png" alt="" />
            </div>
            <div class="logo" v-if="index == 1">
              <span>{{ index + 1 }}</span
              ><img src="../assets/img/rank_book_top_2.png" alt="" />
            </div>
            <div class="logo" v-if="index == 2">
              <span>{{ index + 1 }}</span
              ><img src="../assets/img/rank_book_top_3.png" alt="" />
            </div>
            <div class="logo" v-if="index > 2">
              <span>{{ index + 1 }}</span
              ><img src="../assets/img/rank_book_top_n.png" alt="" />
            </div>
            <router-link
              custom
              :to="'/details?id=' + item.bookId"
              v-slot="{ href, navigate }"
            >
              <div class="img" :href="href" @click="navigate">
                <img :src="item.coverImg" />
              </div>
              <div class="title" :href="href" @click="navigate">
                <h6>{{ item.bookName }}</h6>
                <p>{{ item.authorName }}</p>
              </div>
            </router-link>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.searchView {
  padding: 50px 0;
  .search {
    background-color: #fff;

    z-index: 10;
    position: fixed;
    border-bottom: 2px solid #f7f7f7;
    top: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    align-items: center;
    padding: 8px 15px;
    display: flex;
    justify-content: space-between;
    .center {
      width: 80%;
      position: relative;
      color: #b0b0b5;
      input {
        width: 100%;
        padding-left: 30px;
        box-sizing: border-box;
        font-size: 13px;
        border: none;
        background-color: #f6f6f6;
        border-radius: 8px;
        height: 30px;
      }
      i {
        position: absolute;
        left: 10px;
        top: 50%;
        font-size: 13px;
        transform: translateY(-50%);
      }
    }
  }
  .history {
    padding: 20px 15px;
    h4 {
      width: 100%;
      font-size: 14.5px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      i {
        font-size: 14.5px;
        color: #999;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        display: flex;
        padding: 5px 10px;
        margin-left: 10px;
        border-radius: 10px;
        background-color: #f6f6f6;
        color: #2a2a2a;
        margin-top: 10px;
        font-size: 12px;
      }
    }
  }
  .history1 {
    ul {
      .con {
        color: #ec4f2e;
        
        i {
          margin-right: 5px;
          font-size: 13px;
        }
      }
    }
  }

  .Hotsearch {
    padding: 0 15px;
    .center {
      border-radius: 8px;
      overflow: hidden;
      h3 {
        background-image: linear-gradient(#e7eaee, #def2eb);
        padding: 12px 15px;
        display: flex;
        width: 100%;
        box-sizing: border-box;
        justify-content: space-between;
        align-items: center;
        font-size: 14.5px;
        a {
          font-size: 13px;
          i {
            font-size: 13px;
          }
        }
      }
      ul {
        padding: 11px 0;
        background-image: linear-gradient(#f9fbfa, #f7fbfa);
        li {
          display: flex;
          padding: 0 14px;
          align-items: center;
          .logo {
            position: relative;
            display: inline-block;
            span {
              position: absolute;
              top: 25%;
              display: inline-block;
              left: 50%;
              transform: translateX(-50%);
              font-size: 12px;
              color: #fff;
            }
            height: 25px;
            img {
              height: 100%;
            }
          }
          .img {
            margin: 10px;
            height: 60px;
            img {
              border-radius: 4px;
              height: 100%;
            }
          }
          .title {
            h6 {
              font-size: 13px;
            }
            p {
              margin-top: 12px;
              color: #775a44;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
}
</style>